/*
 * Main.java
 *
 * Created on March 30, 2009, 5:06 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package im;

/**
 *
 * @author bluejack
 */
import im.eventservice.EventServiceListenBehaviour;
import im.eventservice.EventServiceLogBehaviour;
import im.eventservice.OurFormatter;
import im.general.Config;
//import im.node.MobileNodeAgent;
//import jade.core.AgentContainer;
import im.numgenerator.PseudoRandomGenerator;
import jade.core.ProfileImpl;
import jade.core.Runtime;
import jade.wrapper.AgentContainer;
import jade.wrapper.AgentController;
import jade.wrapper.ControllerException;
import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {

    /** Creates a new instance of Main */
    public Main() {
    }

    public static void main(String[] args) {

        /*
         * PseudoRandomGenerator
         */
//creazione directory per file di logging
        boolean success = (new File(Config.LOGGING_DIRECTORY)).mkdir();;

            if (success) {
                System.out.println("Ho creato: " + Config.LOGGING_DIRECTORY);
            } else {
                System.out.println("Impossibile creare: " + Config.LOGGING_DIRECTORY);
            }

        //creazione logger generale
        Logger log = Logger.getLogger("generalLog");
        String slash = null;
        FileHandler fh = null;
        OurFormatter sf = new OurFormatter();

         if(EventServiceLogBehaviour.getOsName().equals("windows")){
            slash = "\\";
        }
        else if(EventServiceLogBehaviour.getOsName().equals("linux")){
            slash = "//";
        }

        try {
            fh = new FileHandler(Config.LOGGING_DIRECTORY + slash + Config.GENERAL_LOGGING_FILE);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        fh.setFormatter(sf);
        log.addHandler(fh);
        log.setLevel(Level.ALL);



        PseudoRandomGenerator alea = new PseudoRandomGenerator(Config.SEED_DISTR);
        //PseudoRandomGenerator.init(Config.SEED_DISTR);

        int num_container = Config.MAX_NUM_CONTEINERS;
        //System.out.println("ciao");

        Runtime imPlatform = Runtime.instance();
        //ResourceMonitor RM = new ResourceMonitor();
        AgentContainer imMainContainer = imPlatform.createMainContainer(new ProfileImpl());
        jade.wrapper.AgentContainer imContainers[] = new jade.wrapper.AgentContainer[num_container];
        // jade.wrapper.ContainerController imContainers[] = imPlatform.createAgentContainer(new ProfileImpl());

        //lancia ogni container dell'array
        for (int i = 0; i < num_container; i++) {
            imContainers[i] = imPlatform.createAgentContainer(new ProfileImpl());
            try {
                System.out.println("Main>\t\t[Iniziale]\tAvvio Container " + i);
                imContainers[i].start();
            } catch (ControllerException e1) {
                e1.printStackTrace();
            }
        }

        //lancia tutti gli agenti al posto giusto
        try {

            //lancia l'RMA nel main container
            AgentController RMA = imMainContainer.createNewAgent("RMA", "jade.tools.rma.rma", null);
            System.out.println("Main>\t\t[Iniziale]\tAvvio RMA");
            RMA.start();

            Object[] argv = new Object[2];

            argv[0] = log;//new String("Container-0");
            argv[1] = new Long(alea.getNextSeed());
            jade.wrapper.AgentController rm = imContainers[0].createNewAgent(Config.EVENT_SERVICE_NAME, "im.eventservice.EventServiceAgent", argv);
            rm.start();

            Thread.sleep(500);

            for (int i = 1; i < num_container; i++) {
                //lancia l'agente resource monitor nei container
                argv[0] = log;//new String("Container-" + i);
                argv[1] = new Long(alea.getNextSeed());
                rm = imContainers[i % num_container].createNewAgent("" + i, "im.node.MobileNodeAgent", argv);
                rm.start();
                
                /*section for time to next node*/
                long tempo=(long) (alea.getPoisson(1000.0 * 10.0));
                System.out.println("New Node: " + i + " after " + tempo + " ms.");
                Thread.sleep(tempo);//
            }

            for(int i=0; i<5;i++) {
            System.out.println("*************************************************");
            Thread.sleep(1000 * 60 * 1);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

  System.exit(0);

        //imPlatform.shutDown();
        
    //System.out.println(imPlatform.getClass().getName());


    }
}
